package com.elf.zero.datetime;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.NumberPicker;

import com.elf.zero.app.AbsLinearLayout;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * 日期选择器
 * Created by Lidong on 2018/12/5.
 */
public class DatePicker extends AbsLinearLayout {

    public final static String format_date = "yyyy-MM-dd";

    private final static int year_min_value = 1900;
    private final static int year_max_value = 2099;

    private NumberPicker np_year, np_month, np_day;

    public DatePicker(Context context) {
        super(context);
    }

    public DatePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutResId() {
        return 0;
    }

    @Override
    protected void initView(AttributeSet attrs) {
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);

        np_year = new NumberPicker(getContext());
        np_month = new NumberPicker(getContext());
        np_day = new NumberPicker(getContext());

        np_year.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
        np_month.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
        np_day.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

        addView(np_year);
        addView(new View(getContext()), 10, 1);
        addView(np_month);
        addView(new View(getContext()), 10, 1);
        addView(np_day);

        initYearData();
        initMonthData();
        initDayData();

        setDate(Calendar.getInstance());

        np_year.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                initDayData();
            }
        });
        np_month.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                initDayData();
            }
        });
    }

    public void setDate(Calendar calendar) {
        setDate(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH) + 1,
                calendar.get(Calendar.DAY_OF_MONTH));
    }

    public void setDate(int year, int month, int day) {
        np_year.setValue(year);
        np_month.setValue(month);
        np_day.setValue(day);
    }

    public Calendar getDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, np_year.getValue());
        calendar.set(Calendar.MONTH, np_month.getValue() - 1);
        calendar.set(Calendar.DAY_OF_MONTH, np_day.getValue());
        return calendar;
    }

    public String getDate(String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);
        return dateFormat.format(getDate().getTime());
    }

    private void initYearData() {
        np_year.setMinValue(year_min_value);
        np_year.setMaxValue(year_max_value);
        String[] displayValues = new String[year_max_value - year_min_value + 1];
        for (int i = 0, j = year_min_value; i < displayValues.length; i++, j++) {
            displayValues[i] = j + "年";
        }
        np_year.setDisplayedValues(displayValues);
    }

    private void initMonthData() {
        np_month.setMinValue(1);
        np_month.setMaxValue(12);
        String[] displayValues = new String[12];
        for (int i = 0; i < displayValues.length; i++) {
            displayValues[i] = i + 1 + "月";
        }
        np_month.setDisplayedValues(displayValues);
    }

    private void initDayData() {
        int year = np_year.getValue();
        int month = np_month.getValue();
        int dayCount = calDayCount(year, month);

        np_day.setDisplayedValues(null);
        String[] displayValues = new String[dayCount];
        np_day.setMinValue(1);
        np_day.setMaxValue(dayCount);
        for (int i = 0; i < displayValues.length; i++) {
            displayValues[i] = i + 1 + "日";
        }
        np_day.setDisplayedValues(displayValues);
    }

    private int calDayCount(int year, int month) {
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;

            case 2:
                return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 29 : 28;

            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
        }

        return 0;
    }
}
